let USER_KEY;
USER_KEY = 'user';
let TOKEN_KEY;
TOKEN_KEY = 'token';

class Auth{
    constructor() {
        this.token = null;
        this.user = null;
        const userJson = window.localStorage.getItem(USER_KEY);
        this.token = window.localStorage.getItem(TOKEN_KEY);
        if(userJson !== null){
            this.user = JSON.parse(userJson)
        }
    }

    static getInstance(){
        if(!this._instance){
            this._instance = new Auth()
        }
        return this._instance
    }

    setUserAndToken(user, token){
        this.user = user;
        this.token = token;
        window.localStorage.setItem(USER_KEY,JSON.stringify(user));
        window.localStorage.setItem(TOKEN_KEY,token);
    }

    clearUserAndToken(){
        this.token = null;
        this.user = null;
        window.localStorage.removeItem(USER_KEY)
        window.localStorage.removeItem(TOKEN_KEY)
    }

    get isAuthed(){
        return !!(this.user && this.token);
    }
}



export default new Auth()
